<?php

namespace Admin\Controller;

use Think\Controller;

class ShareorderController extends RightController
{
    // 加载初始化内容页,并检索
    public function index()
    {
        //判断分页时搜索条件
        if (I("get.p")) {
            $where = session("QG_SEARCH_WHERE_shareorder");
            $params = session("QG_SEARCH_PARAMS_shareorder");
            $this->assign("params", session("QG_SEARCH_PARAMS_shareorder"));
            $where = empty($where) ? "1" : $where;
            $pageSize = session("QG_SEARCH_SIZE_shareorder");
        } else {
            //清除session存储
            session("QG_SEARCH_WHERE_shareorder", null);
            session("QG_SEARCH_PARAMS_shareorder", null);
            session("QG_SEARCH_SIZE_shareorder", null);
            $pageSize = I("post.pageSize");
            $params = I("post.");

            $where = "1";
        }

        // POST数据提交，重新组合检索条件
        if (IS_POST) {
            unset($_GET["p"]);
            $where = $this->searchCondition();
        }

        // 分页输入
        if (empty($pageSize)) {
            $pageSize = 10;
        }

        // 总条数
        $count = M('myshare')
            ->alias("t")
            ->field("t.id,t.createTime,m.account,m.realName,s.name,s.type,t.status")
            ->join(" LEFT JOIN t_member m ON m.id = t.memberId")
            ->join(" LEFT JOIN t_share s ON s.id = t.shareId")
            ->where($where)
            ->count();
        $page = new \Admin\Model\Page($count, $pageSize);
        $show = $page->show();

        // 进行分页数据查询
        $list = M('myshare')
            ->alias("t")
            ->field("t.id,t.createTime,m.account,m.realName,s.name,s.type,t.status")
            ->join(" LEFT JOIN t_member m ON m.id = t.memberId")
            ->join(" LEFT JOIN t_share s ON s.id = t.shareId")
            ->where($where)
            ->order('t.id DESC')
            ->limit($page->firstRow . ',' . $page->listRows)->select();

        // 渲染查询条件到session
        $lists = array(
            "page" => getDropdownList("pageSize", "", false),
            "sharetype" => getDropdownList("shareType"),
            "status" => getDropdownList("shareorderType"),
        );
        session("QG_SEARCH_WHERE_shareorder", $where);
        session("QG_SEARCH_PARAMS_shareorder", $params);
        session("QG_SEARCH_SIZE_shareorder", $pageSize);

        // 输出数据
        $this->assign("params", $params);
        $this->assign('list', $list);
        $this->assign('lists', $lists);
        $this->assign('page', $show);
        $this->display();
    }

    // 搜索条件
    public function searchCondition()
    {
        $where = "1";

        // 账号
        if (I('post.account')) {
            // 精确
            if (I('post.accountCondition') == 1) {
                $where .= " AND m.account='" . I("post.account") . "'";
            } else {
                $where .= " AND m.account LIKE '%" . I("post.account") . "%'";
            }
        }

        // 活动名称
        if (I('post.name')) {
            // 精确
            if (I('post.nameCondition') == 1) {
                $where .= " AND s.name='" . I("post.name") . "'";
            } else {
                $where .= " AND s.name LIKE '%" . I("post.name") . "%'";
            }
        }

        // 创建时间
        if (I('post.createtimeStart') || I('post.createtimeEnd')) {
            // 起始日期
            if (I('post.createtimeStart')) {
                $where .= " AND t.createTime>='" . I("post.createtimeStart") . "'";
            }
            // 结束日期
            if (I('post.createtimeEnd')) {
                $where .= " AND t.createTime<='" . I("post.createtimeEnd") . " 23:59:59'";
            }
        }

        //共享类型
        if (I('post.sharetype')) {
            $where .= " AND s.type='" . I("post.sharetype") . "'";
        }

        //订单状态
        if (I('post.status')) {
            $where .= " AND t.status='" . I("post.status") . "'";
        }

        return $where;
    }

    // 删除
    public function delete()
    {
        if (IS_POST) {
            // ID
            $id = intval(I("post.id"));

            // 没有ID
            if (empty($id)) {
                $this->ajaxError("错误！");
            }
            //删除数据
            $result = M('myshare')->where(array("id" => $id))->delete();

            // 如果删除成功
            if ($result) {
                $this->ajaxSuccess("删除成功！");
            } else {
                $this->ajaxError("删除失败！");
            }
        }
    }

    // 完成订单
    public function check()
    {
        if (IS_POST) {
            // ID
            $id = intval(I("post.id"));

            // 没有ID
            if (empty($id)) {
                $this->ajaxError("错误！");
            }
            //删除数据
            $result = M('myshare')->where(array("id" => $id))->save(array("status" => 2));

            // 如果删除成功
            if ($result) {
                $this->ajaxSuccess("操作成功！");
            } else {
                $this->ajaxError("操作失败！");
            }
        }
    }

}
